package kr.hkit.powerjava.chther22;

import java.util.Arrays;

public class Array {
	public static<T> T getLast(T[] a){
		return a[a.length-1];
	}
	public static <T extends Comparable<T>> T getMax(T[] a){
		if(a == null || a.length ==0 ) return null;
		T largest = a[0];
		for(int i =1; i<a.length;i++){
			if(largest.compareTo(a[i])<0){
				largest = a[i];
				return largest;
			}
		}
		return largest;
	}
	
	public static <T extends Comparable<T>> T getMin(T[] a){
		if(a == null || a.length ==0 ) return null;
		
			T smallest = a[0];
			for(int i =1; i<a.length;i++){
				if(smallest.compareTo(a[i])>0){
					smallest = a[i];
					return smallest;
				}
		
			}
			return smallest;
		
		
		
		
	}
	
	
	public static void main(String[] args) {
	
				String[] language ={"JAVA","C#","C++","BASIC"};
				Integer[] arr = {5,4,3,2,1,9};
				
				String last = Array.getLast(language);
				System.out.println(last);
				int aLast = Array.getLast(arr);
				System.out.println(aLast);
				
				int aMax = Array.getMax(arr);
				int aMin = Array.getMin(arr);
				System.out.println(aMax);
				System.out.println(aMin);
				
				Test[] arrTest = new Test[3];
				arrTest[0] = new Test(5);
				arrTest[1] = new Test(4);
				arrTest[2] = new Test(3);
				System.out.println(Arrays.toString(arrTest)+""+Array.getMax(arrTest));
				System.out.println(Arrays.toString(arrTest)+""+Array.getMin(arrTest));
	}
}
class Test implements Comparable<Test>{
	@Override
	public String toString() {
		return "Test [a=" + a + "]";
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	public Test(int a) {
		this.a = a;
	}

	private int a;

	@Override
	public int compareTo(Test o) {
		// TODO Auto-generated method stub
		return a-o.a;
	}
}

